package com.sunxd.vue;

import com.alibaba.fastjson.JSON;
import com.sunxd.vue.domain.bean.domain.Student;
import com.sunxd.vue.domain.bean.domain.Teacher;
import com.sunxd.vue.domain.bean.domain.User;
import com.sunxd.vue.infrastructure.config.AssistConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @author sun.xd
 * @description: description
 * @date 2024/4/2 9:24
 */
@SpringBootApplication
public class ZStudyVueApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(ZStudyVueApplication.class, args);
//        User user = applicationContext.getBean(User.class);
//        System.out.println(JSON.toJSONString(user));
//
//        Teacher teacher = applicationContext.getBean(Teacher.class);
//        System.out.println(JSON.toJSONString(teacher));

        AssistConfig user = applicationContext.getBean(AssistConfig.class);
        System.out.println(JSON.toJSONString(user));
    }


    /**
     * ConditionalOnProperty
     * 如果配置文件中有 test1.c1 ,和test1.c2 时， 则进行创建该对象
     *
     * @param name
     * @return
     */
    @Bean
//    @ConditionalOnProperty(value ={"test1.c1","test1.c2"} )  2种写法都可以
    @ConditionalOnProperty(prefix = "test1", value = {"c1", "c2"})
    public User getUser(@Value("${test1.c1}") String name) {
        return new User();
    }


    // ConditionalOnBean  当某个类存在时，则加载。。 如果学生类不存在，则不加载下面的老师类
//    @ConditionalOnClass 与之类似，，里面只有类和名称2种参数
    @Bean
    public Student getStudent() {
        return Student.builder().name("Student").build();
    }
//    @Bean
//    @ConditionalOnBean(value = {Student.class})
//    public Teacher getTeacher(Student student){
//        return Teacher.builder().name("teacher").build();
//    }
    // 使用类型进行加载
//    @Bean
//    @ConditionalOnBean(type = {"com.sunxd.vue.domain.bean.domain.Student"})
//    public Teacher getTeacher(Student student){
//        return Teacher.builder().name("teacher2").build();
//    }
//    @Bean
//    @ConditionalOnBean(name = {"getStudent"})
//    public Teacher getTeacher(Student student){
//        return Teacher.builder().name("teacher3").build();
//    }


}
